/*
 * FaceCat图形通讯框架(非开源)
 * 著作权编号:2015SR229355+2020SR0266727
 * 上海卷卷猫信息技术有限公司
 */

#include "stdafx.h"
#include "FCCheckBox.h"

namespace FaceCat{
    Long FCCheckBox::getPaintingBackColor(){
        Long backColor = getBackColor();
        if (backColor != FCColor_None && FCColor_DisabledBack != FCColor_None){
            if (!isPaintEnabled(this)){
                return FCColor_DisabledBack;
            }
        }
        return backColor;
    }
    
    Long FCCheckBox::getPaintingButtonBackColor(){
        Long buttonBackColor = m_buttonBackColor;
        if (buttonBackColor != FCColor_None && FCColor_DisabledBack != FCColor_None){
            if (!isPaintEnabled(this)){
                return FCColor_DisabledBack;
            }
        }
        return buttonBackColor;
    }
    
    Long FCCheckBox::getPaintingButtonBorderColor(){
        return m_buttonBorderColor;
    }
    
    String FCCheckBox::getPaintingBackImage(){
        String backImage;
        if (m_checked){
            if (isEnabled()){
                FCNative *native = getNative();
                if (this == native->getPushedView()){
                    backImage = m_checkPushedBackImage;
                }
                else if (this == native->getHoveredView()){
                    backImage = m_checkHoveredBackImage;
                }
                else{
                    backImage = m_checkedBackImage;
                }
            }
            else{
                backImage = m_disableCheckedBackImage;
            }
        }
        if((int)backImage.length() > 0){
            return backImage;
        }
        else{
            return FCButton::getPaintingBackImage();
        }
    }
    
    
    //////////////////////////////////////////////////////////////////////////////////////////////////////
    
    FCCheckBox::FCCheckBox(){
        m_buttonAlign = FCHorizontalAlign_Left;
        m_buttonBackColor = FCColor_Border;
        m_buttonBorderColor = FCColor_Border;
        m_buttonSize.cx = 16;
        m_buttonSize.cy = 16;
        m_checked = false;
        setBackColor(FCColor_None);
        setBorderColor(FCColor_None);
    }
    
    FCCheckBox::~FCCheckBox(){
    }
    
    FCHorizontalAlign FCCheckBox::getButtonAlign(){
        return m_buttonAlign;
    }
    
    void FCCheckBox::setButtonAlign(FCHorizontalAlign value){
        m_buttonAlign = value;
    }
    
    Long FCCheckBox::getButtonBackColor(){
        return m_buttonBackColor;
    }
    
    void FCCheckBox::setButtonBackColor(Long value){
        m_buttonBackColor = value;
    }
    
    Long FCCheckBox::getButtonBorderColor(){
        return m_buttonBorderColor;
    }
    
    void FCCheckBox::setButtonBorderColor(Long value){
        m_buttonBorderColor = value;
    }
    
    FCSize FCCheckBox::getButtonSize(){
        return m_buttonSize;
    }
    
    void FCCheckBox::setButtonSize(FCSize value){
        m_buttonSize = value;
    }
    
    bool FCCheckBox::isChecked(){
        return m_checked;
    }
    
    void FCCheckBox::setChecked(bool value){
        if(m_checked != value){
            m_checked = value;
            onCheckedChanged();
        }
    }
    
    String FCCheckBox::getCheckedBackImage(){
        return m_checkedBackImage;
    }
    
    void FCCheckBox::setCheckedBackImage(const String& value){
        m_checkedBackImage = value;
    }
    
    String FCCheckBox::getCheckHoveredBackImage(){
        return m_checkHoveredBackImage;
    }
    
    void FCCheckBox::setCheckHoveredBackImage(const String& value){
        m_checkHoveredBackImage = value;
    }
    
    String FCCheckBox::getCheckPushedBackImage(){
        return m_checkPushedBackImage;
    }
    
    void FCCheckBox::setCheckPushedBackImage(const String& value){
        m_checkPushedBackImage = value;
    }
    
    String FCCheckBox::getDisableCheckedBackImage(){
        return m_disableCheckedBackImage;
    }
    
    void FCCheckBox::setDisableCheckedBackImage(const String& value){
        m_disableCheckedBackImage = value;
    }
    
    //////////////////////////////////////////////////////////////////////////////////////////////////
    String FCCheckBox::getViewType(){
        return L"CheckBox";
    }
    
    void FCCheckBox::getAttribute(const String& name, String *value, String *type){
        if (name == L"buttonalign"){
            *type = L"enum:FCHorizontalAlign";
            *value = FCTran::horizontalAlignToStr(getButtonAlign());
        }
        else if(name == L"buttonsize"){
            *type = L"size";
            *value = FCTran::sizeToStr(getButtonSize());
        }
        else if(name == L"checked"){
            *type = L"bool";
            *value = FCTran::boolToStr(isChecked());
        }
        else if(name == L"checkedbackimage"){
            *type = L"text";
            *value = getCheckedBackImage();
        }
        else if(name == L"checkhoveredbackimage"){
            *type = L"text";
            *value = getCheckHoveredBackImage();
        }
        else if(name == L"checkpushedbackimage"){
            *type = L"text";
            *value = getCheckPushedBackImage();
        }
        else if(name == L"disablecheckedbackimage"){
            *type = L"text";
            *value = getDisableCheckedBackImage();
        }
        else{
            FCButton::getAttribute(name, value, type);
        }
    }
    
    ArrayList<String> FCCheckBox::getAttributeNames(){
        ArrayList<String> attributeNames = FCButton::getAttributeNames();
        attributeNames.add(L"ButtonAlign");
        attributeNames.add(L"ButtonSize");
        attributeNames.add(L"Checked");
        attributeNames.add(L"CheckedBackImage");
        attributeNames.add(L"CheckHoveredBackimage");
        attributeNames.add(L"CheckPushedBackImage");
        attributeNames.add(L"DisableCheckedBackImage");
        return attributeNames;
    }

    ArrayList<String> FCCheckBox::getEventNames(){
        ArrayList<String> eventNames = FCButton::getEventNames();
        eventNames.add(L"CheckedChanged");
        return eventNames;
    }
    
    void FCCheckBox::onCheckedChanged(){
        callEvents(L"oncheckedchanged");
        update();
    }
    
    void FCCheckBox::onClick(FCTouchInfo touchInfo){
        setChecked(!isChecked());
        callTouchEvents(L"onclick", touchInfo);
        invalidate();
    }
    
    void FCCheckBox::onPaintBackground(FCPaint *paint, const FCRect& clipRect){
        FCRect rect ={0, 0, getWidth(), getHeight()};
        paint->fillRoundRect(getPaintingBackColor(), rect, getCornerRadius());
    }
    
    void FCCheckBox::onPaintCheckButton(FCPaint *paint, const FCRect& clipRect){
        String bkImage = getPaintingBackImage();
        if ((int)bkImage.length() > 0){
            paint->drawImage(bkImage, clipRect);
        }
        else{
            if(m_checked){
                FCRect innerRect = {clipRect.left + 2, clipRect.top + 2, clipRect.right - 2, clipRect.bottom - 2};
                if (clipRect.right - clipRect.left < 4 || clipRect.bottom - clipRect.top < 4){
                    innerRect = clipRect;
                }
                FCPoint pp[3] = {0};
                pp[0].x = innerRect.left;
                pp[0].y = innerRect.top + 8;
                pp[1].x = innerRect.left + 6;
                pp[1].y = innerRect.bottom;
                pp[2].x = innerRect.right - 1;
                pp[2].y = innerRect.top;
                paint->drawPolyline(getPaintingTextColor(), 1, 0, pp, 3);
            }
            paint->drawRect(getPaintingButtonBorderColor(), 1, 0, clipRect);
        }
    }
    
    void FCCheckBox::onPaintForeground(FCPaint *paint, const FCRect& clipRect){
        String text = getText();
        int width = getWidth(), height = getHeight();
        FCRect buttonRect ={5, (height - m_buttonSize.cy) / 2, 5 + m_buttonSize.cx, (height + m_buttonSize.cy) / 2};
        FCPoint tLocation ={0};
        FCSize tSize ={0};
        FCFont *font = getFont();
        if ((int)text.length() > 0){
            tSize = paint->textSize(text, font, -1);
            tLocation.x = buttonRect.right + 5;
            tLocation.y = (height - tSize.cy) / 2;
        }
        if (m_buttonAlign == FCHorizontalAlign_Center){
            buttonRect.left = (width - m_buttonSize.cx) / 2;
            buttonRect.right = (width + m_buttonSize.cx) / 2;
            tLocation.x = buttonRect.right + 5;
        }
        else if (m_buttonAlign == FCHorizontalAlign_Right){
            buttonRect.left = width - m_buttonSize.cx - 5;
            buttonRect.right = width - 5;
            tLocation.x = buttonRect.left - tSize.cx - 5;
        }
        onPaintCheckButton(paint, buttonRect);
        if ((int)text.length() > 0){
            FCRect tRect ={tLocation.x, tLocation.y, tLocation.x + tSize.cx + 1, tLocation.y + tSize.cy};
            Long textColor = getPaintingTextColor();
            if(autoEllipsis() && (tRect.right > clipRect.right || tRect.bottom > clipRect.bottom)){
                if (tRect.right > clipRect.right) {
                    tRect.right = clipRect.right;
                }
                if (tRect.bottom > clipRect.bottom) {
                    tRect.bottom = clipRect.bottom;
                }
                paint->drawTextAutoEllipsis(text, textColor, font, tRect);
            }
            else{
                paint->drawText(text, textColor, font, tRect, -1);
            }
        }
    }

    void FCCheckBox::onScaling(double scaleFactorX, double scaleFactorY) {
        FCView::onScaling(scaleFactorX, scaleFactorY);
        m_buttonSize.cx = (int)(m_buttonSize.cx * scaleFactorX);
        m_buttonSize.cy = (int)(m_buttonSize.cy * scaleFactorY);
    }
    
    void FCCheckBox::setAttribute(const String& name, const String& value){
        if (name == L"buttonalign"){
            setButtonAlign(FCTran::strToHorizontalAlign(value));
        }
        else if(name == L"buttonsize"){
            setButtonSize(FCTran::strToSize(value));
        }
        else if(name == L"checked"){
            setChecked(FCTran::strToBool(value));
        }
        else if(name == L"checkedbackimage"){
            setCheckedBackImage(value);
        }
        else if(name == L"checkhoveredbackimage"){
            setCheckHoveredBackImage(value);
        }
        else if(name == L"checkpushedbackimage"){
            setCheckPushedBackImage(value);
        }
        else if(name == L"disablecheckedbackimage"){
            setDisableCheckedBackImage(value);
        }
        else{
            FCButton::setAttribute(name, value);
        }
    }
}
